//
//  DecryptProtectView.m
//  MyProject
//
//  Created by 石小强 on 2018/4/24.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "DecryptProtectView.h"
#import "HCSStarRatingView.h"

@interface DecryptProtectView()

@property (nonatomic, strong) UIImageView *headerIV;
@property (nonatomic, strong) UILabel *nameL;
@property (nonatomic, strong) HCSStarRatingView *starView;
@property (nonatomic, strong) UILabel *descL;
@property (nonatomic, strong) UILabel *replyL;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UILabel *categoryL;
@property (nonatomic, strong) UILabel *timeL;

@end

@implementation DecryptProtectView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    UIImageView *headerIV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 39, 39)];
//    headerIV.image = [UIImage imageNamed:@"公司icon"];
    headerIV.layer.cornerRadius = 19.5;
    headerIV.layer.masksToBounds = YES;
    self.headerIV = headerIV;
    [self addSubview:headerIV];
    headerIV.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick)];
    [headerIV addGestureRecognizer:tgr];
    
    UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(headerIV.frame) + 6, 15, 100, 39)];
//    nameL.text = self.evaluateModel.name;
    nameL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    nameL.textColor = UIColorFromRGB(0x48565A);
    self.nameL = nameL;
    [self addSubview:nameL];
    
    HCSStarRatingView *starView = [[HCSStarRatingView alloc] initWithFrame:CGRectMake(kScreenWidth - 95, 30, 80, 12)];
    starView.emptyStarColor = UIColorFromRGB(0xBFBFBF);
    starView.emptyStarImage = [UIImage imageNamed:@"ic_stars_没选中没选中Unselected"];
    starView.filledStarImage = [UIImage imageNamed:@"ic_stears_Selected"];
    starView.halfStarImage = [UIImage imageNamed:@"ic_stars_Halfselected"];
    starView.maximumValue = 5;
    starView.minimumValue = 0;
//    starView.value = [self.evaluateModel.starValue floatValue];
    [starView addTarget:self action:@selector(starViewValueChanged:) forControlEvents:UIControlEventValueChanged];
    starView.allowsHalfStars = YES;
    starView.accurateHalfStars = YES;
    starView.continuous = YES;
    self.starView = starView;
    [self addSubview:starView];
    
//    CGSize descS = [Tool sizeWithText:self.evaluateModel.desc font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] maxW:kScreenWidth - 30];
    UILabel *descL = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(headerIV.frame) + 10, kScreenWidth - 30, 50)];
    descL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
//    descL.text = self.evaluateModel.desc;
    descL.textColor = UIColorFromRGB(0x2B3033);
    descL.numberOfLines = 0;
    self.descL = descL;
    [self addSubview:descL];
    
    UILabel *replyL = [[UILabel alloc] init];
    replyL.frame = CGRectMake(15, CGRectGetMaxY(descL.frame) + 7.5, kScreenWidth - 30, 12);
    self.replyL = replyL;
    [self addSubview:replyL];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(replyL.frame) + 7.5, kScreenWidth, 1)];
    line.backgroundColor = UIColorFromRGB(0xE5ECEE);
    self.line = line;
    [self addSubview:line];
    
    UILabel *categoryL = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(line.frame), 200, 30)];
    categoryL.text = @"服务类型：JAVA";
    categoryL.font = [UIFont fontWithName:@"PingFangSC-Light" size:12];
    categoryL.textColor = UIColorFromRGB(0x8DA2AC);
    self.categoryL = categoryL;
    [self addSubview:categoryL];
    
    UILabel *timeL = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 165, CGRectGetMaxY(line.frame), 150, 30)];
//    timeL.text = self.evaluateModel.time;
    timeL.font = [UIFont fontWithName:@"PingFangSC-Light" size:12];
    timeL.textColor = UIColorFromRGB(0x8DA2AC);
    timeL.textAlignment = NSTextAlignmentRight;
    self.timeL = timeL;
    [self addSubview:timeL];
}

- (void)starViewValueChanged:(HCSStarRatingView *)starView {
    self.starView.value = [self.model.star floatValue];
}

- (void)setModel:(CommentModel *)model {
    _model = model;
    
    [self.headerIV sd_setImageWithURL:ImageURLWithKey(model.publisherHeadImg) placeholderImage:PlaceholderImagePeople];
    self.nameL.text = model.publisher;
    self.starView.value = [model.star floatValue];
    
    // 内容
    CGSize descS = [Tool sizeWithText:model.content font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] maxW:kScreenWidth - 30];
    self.descL.text = model.content;
    self.descL.frame = CGRectMake(15, 65, kScreenWidth - 30, descS.height);
    
    // 同事回复
    NSString *replyStr;
    CGFloat replySizeHeight = 0;
    if (model.comments.count > 0) {
        CommentModel *comment = [model.comments objectAtIndex:0];
        if (comment.content && comment.content.length > 0) {
            replyStr = [NSString stringWithFormat:@"同事回复：%@", comment.content];
            replySizeHeight = [Tool sizeWithText:replyStr font:[UIFont fontWithName:@"PingFang SC" size:12] maxW:kScreenWidth - 30].height;
        } else {
            replyStr = @"";
            replySizeHeight = 0;
        }
    } else {
        replyStr = @"";
        replySizeHeight = 0;
    }
    
    self.replyL.text = replyStr;
    self.replyL.frame = CGRectMake(15, CGRectGetMaxY(self.descL.frame) + 7.5, kScreenWidth - 30, replySizeHeight);
    
    self.line.frame = CGRectMake(0, CGRectGetMaxY(self.replyL.frame) + 7.5, kScreenWidth, 1);
    
    self.categoryL.frame = CGRectMake(15, CGRectGetMaxY(self.line.frame), 200, 30);
    self.categoryL.text = [NSString stringWithFormat:@"服务类型：%@", model.targetPositon];
    if (model.targetPositon && model.targetPositon.length > 0) {
        self.categoryL.hidden = NO;
    } else {
        self.categoryL.hidden = YES;
    }
    
    NSArray *timeArr = [model.releaseTime componentsSeparatedByString:@" "];
    if (timeArr.count == 2) {
        self.timeL.text = [timeArr objectAtIndex:0];
    } else {
        self.timeL.text = model.releaseTime;
    }
    
    self.timeL.frame = CGRectMake(kScreenWidth - 165, CGRectGetMaxY(self.line.frame), 150, 30);
}

- (void)iconClick {
    self.headerIconBlock(self.model.publisherId);
}

@end
